package com.apes.framework.jpa;

import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.plugin.soe.core.SceneEngine;
import com.apes.framework.plugin.soe.metadata.Scene;
import com.apes.framework.plugin.soe.metadata.SceneInstance;
import com.apes.framework.plugin.soe.store.database.service.SceneInstanceService;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;

import static java.text.MessageFormat.format;

/**
 * 功能：
 *
 * @author xul
 * @create 2018-01-25 15:56
 */
@Getter
@Setter
@MappedSuperclass
public class Model extends BaseModel {

//    @ManyToOne(fetch = FetchType.LAZY)
//    @JoinColumn(name = "creator_id", referencedColumnName = "id", updatable = false)
//    @Comment("创建人")
//    private PartyRole creator;

    @Column(name = "creator_id", length = 64)
    @Comment("创建人")
    private String creator;

//    @ManyToOne(fetch = FetchType.LAZY)
//    @JoinColumn(name = "updater_id", referencedColumnName = "id", insertable = false)
//    @Comment("最后更新人")
//    private PartyRole updater;

    @Column(name = "updater_id", length = 64)
    @Comment("最后更新人")
    private String updater;

    @Version
    @Column(name = "version", columnDefinition = "number(10) default 0")
    @Comment("版本号")
    private int version;  //JPA乐观锁

    @Column(name = "inner_business_type", length = 64)
    @Comment("单据类型")
    private String innerBusinessType; //内部单据类型

//    @Transient
//    private PartyRole operator;  //当前操作员

    @Transient
    private String operator;  //当前操作人（PartyRole.id）

    @Transient
    private String userId;  //当前操作员（User.id）

    @Transient
    private String userName;  //当前操作员（User.name）

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "scene_id", referencedColumnName = "id")
    @Comment("场景")
    private Scene scene;  //场景

//    @Column(name = "scene_id", length = 32)
//    @Comment("场景")
//    private String scene;  //场景


    @Transient
    private String sceneInstanceId;  //场景实例Id

    @Transient
    public String getSceneInstanceId() {
        SceneInstance sceneInstance = sceneInstance();
        if (sceneInstance == null) return null;
        return sceneInstance.getId();
    }

    @Transient
    public SceneInstance sceneInstance() {
        if (getScene() == null) return null;
        SceneEngine engine = getSceneEngine();
        SceneInstance sceneInstance = engine.getRuntimeService().findSceneInstance(getScene().getId(), getInnerBusinessType(), getInnerBusinessKey());
        return sceneInstance;
    }

    @Transient
    private SceneEngine getSceneEngine() {
        return SpringManager.getBean(SceneEngine.class);
    }

    public void createSceneRole(SceneInstance sceneInstance) {
        if (sceneInstance == null) return;
        SceneEngine engine = getSceneEngine();
        engine.getRuntimeService().createSceneInstanceRole(sceneInstance, getInnerBusinessType(), getInnerBusinessKey());
    }

    /**
     * 设置场景变量
     **/
    protected void setSceneInstanceVariable(String name, String text) {
        SceneInstance sceneInstance = sceneInstance();
        if (sceneInstance == null) {
            throw new RuntimeException(format("单据[{0}]：{1} 的场景实例不存在！ ", getInnerBusinessType(), getInnerBusinessKey()));
        }
        SpringManager.getBean(SceneInstanceService.class).setSceneInstanceVariable(sceneInstance, name, text);
    }
}
